home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / Frontier / Frontier SDK 2.1 / Toolkits / Applet Toolkit / appletmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-11  |  7.4 KB  |  418 lines  |  [TEXT/KAHL]

  1.  
  2. /*© Copyright 1988-1992 UserLand Software, Inc.  All Rights Reserved.*/
  3.  
  4.  
  5. #include "appletstrings.h"
  6. #include "appletmemory.h"
  7.  
  8.  
  9. void moveleft (psource, pdest, length) void *psource, *pdest; long length; {
  10.     
  11.     /*
  12.     do a mass memory move with the left edge leading.  good for closing
  13.     up a gap in a buffer, among other things…
  14.     */
  15.     
  16.     register char *ps, *pd;
  17.     register long ctloops;
  18.     
  19.     ctloops = length;
  20.     
  21.     if (ctloops > 0) {
  22.     
  23.         ps = psource; /*copy into a register*/
  24.     
  25.         pd = pdest; /*copy into a register*/
  26.     
  27.         while (ctloops--) *pd++ = *ps++;
  28.         }
  29.     } /*moveleft*/
  30.     
  31.     
  32. void moveright (psource, pdest, length) void *psource, *pdest; long length; {
  33.     
  34.     /*
  35.     do a mass memory move with the right edge leading.  good for opening
  36.     up a gap in a buffer, among other things…
  37.     */
  38.     
  39.     register char *ps, *pd;
  40.     register long ctloops;
  41.     
  42.     ctloops = length;
  43.     
  44.     if (ctloops > 0) {
  45.     
  46.         ps = (char *) psource + length - 1; /*right edge of source*/
  47.     
  48.         pd = (char *) pdest + length - 1; /*right edge of destination*/
  49.     
  50.         while (ctloops--) *pd-- = *ps--;
  51.         }
  52.     } /*moveright*/
  53.     
  54.     
  55. void fillchar (pfill, ctfill, chfill) void *pfill; long ctfill; char chfill; {
  56.     
  57.     /*
  58.     do a mass memory fill -- copy ctfill chfills at pfill.
  59.     */
  60.     
  61.     register char *p = pfill;
  62.     register long ct = ctfill;
  63.     register char ch = chfill;
  64.     
  65.     while (ct--) *p++ = (char) ch; /*tight loop*/
  66.     } /*fillchar*/
  67.     
  68.  
  69. void clearbytes (pclear, ctclear) void *pclear; long ctclear; {
  70.     
  71.     /*
  72.     fill memory with 0's.
  73.     */
  74.     
  75.     fillchar (pclear, ctclear, (char) 0);
  76.     } /*clearbytes*/
  77.     
  78.  
  79. void disposehandle (h) Handle h; {
  80.     
  81.     /*
  82.     our own bottleneck for this built-in.  we don't require type coercion
  83.     and we check if its nil.
  84.     */
  85.     
  86.     if (h != nil)
  87.         DisposHandle (h);
  88.     } /*disposehandle*/
  89.     
  90.     
  91. long gethandlesize (Handle h) {
  92.     
  93.     return (GetHandleSize (h));
  94.     } /*gethandlesize*/
  95.     
  96.     
  97. boolean sethandlesize (Handle h, long size) {
  98.     
  99.     SetHandleSize (h, size);
  100.     
  101.     return (MemError () == noErr);
  102.     } /*gethandlesize*/
  103.     
  104.     
  105. boolean copyhandle (horig, hcopy) Handle horig, *hcopy; {
  106.     
  107.     register Handle h;
  108.     register long ct;
  109.     
  110.     if (horig == nil) { /*6/18/92 DW -- copying a nil handle begets a nil handle, returns true*/
  111.         
  112.         *hcopy = nil;
  113.         
  114.         return (true);
  115.         }
  116.     
  117.     h = NewHandle (ct = gethandlesize (horig));
  118.     
  119.     if (h == nil)
  120.         return (false);
  121.         
  122.     moveleft (*horig, *h, ct);
  123.     
  124.     *hcopy = h;
  125.     
  126.     return (true);
  127.     } /*copyhandle*/
  128.     
  129.  
  130. boolean enlargehandle (Handle hgrow, long ctgrow, ptrchar newdata) {
  131.     
  132.     /*
  133.     make the handle big enough to hold the new data, and move the new data in
  134.     at the end of the newly enlarged handle.
  135.     */
  136.     
  137.     register Handle h = hgrow;
  138.     register long ct = ctgrow;
  139.     register ptrchar p = newdata;
  140.     register long origsize;
  141.     
  142.     origsize = gethandlesize (h);
  143.     
  144.     sethandlesize (h, origsize + ct);
  145.         
  146.     if (MemError () != noErr)
  147.         return (false);
  148.         
  149.     moveleft (p, *h + origsize, ct);
  150.     
  151.     return (true);
  152.     } /*enlargehandle*/ 
  153.     
  154.     
  155. boolean loadfromhandle (hload, ixload, ctload, pdata) Handle hload; long *ixload, ctload; ptrchar pdata; {
  156.     
  157.     /*
  158.     copy the next ctload bytes from hload into pdata and increment the index.
  159.     
  160.     return false if there aren't enough bytes.
  161.     
  162.     start ixload at 0.
  163.     */
  164.     
  165.     register Handle h = hload;
  166.     register ptrchar p = pdata;
  167.     register long ct = ctload;
  168.     register long ix = *ixload;
  169.     register long size;
  170.     
  171.     size = gethandlesize (h);
  172.     
  173.     if ((ix + ct) > size) /*asked for more bytes than there are*/
  174.         return (false); 
  175.         
  176.     moveleft (*h + ix, p, ct); /*copy out of the handle*/
  177.     
  178.     *ixload = ix + ct; /*increment the index into the handle*/
  179.     
  180.     return (true);
  181.     } /*loadfromhandle*/
  182.     
  183.  
  184. boolean newtexthandle (bs, htext) bigstring bs; Handle *htext; {
  185.     
  186.     /*
  187.     create a new handle to hold the text of the string.
  188.     
  189.     if the string is "\pABC" -- you get a handle of size 3.
  190.     */
  191.     
  192.     register long len;
  193.     register Handle h;
  194.     
  195.     h = NewHandle (len = stringlength (bs));
  196.     
  197.     if (h == nil)
  198.         return (false);
  199.     
  200.     if (len > 0)
  201.         moveleft (&bs [1], *h, len);
  202.     
  203.     *htext = h; /*pass handle back to caller*/
  204.     
  205.     return (true);
  206.     } /*newtexthandle*/
  207.  
  208.  
  209. boolean pushtexthandle (bs, htext) bigstring bs; Handle htext; {
  210.     
  211.     /*
  212.     htext is a handle created with newtexthandle.
  213.     
  214.     increase the size of the handle so we can push the text of bs at 
  215.     the end of the handle (not including length byte).
  216.     */
  217.     
  218.     return (enlargehandle (htext, (long) stringlength (bs), (ptrchar) bs + 1));
  219.     } /*pushtexthandle*/
  220.  
  221.  
  222. boolean pushhandleonhandle (Handle hsource, Handle hdest) {
  223.     
  224.     boolean fl;
  225.     
  226.     if (hsource == nil)
  227.         return (true);
  228.     
  229.     lockhandle (hsource);
  230.     
  231.     fl = enlargehandle (hdest, gethandlesize (hsource), *hsource);
  232.     
  233.     unlockhandle (hsource);
  234.     
  235.     return (fl);
  236.     } /*pushtexthandle*/
  237.  
  238.  
  239. void texthandletostring (Handle htext, bigstring bs) {
  240.     
  241.     long len;
  242.     
  243.     if (htext == nil) {
  244.         
  245.         setstringlength (bs, 0);
  246.         
  247.         return;
  248.         }
  249.     
  250.     len = gethandlesize (htext);
  251.     
  252.     if (len > lenbigstring)
  253.         len = lenbigstring;
  254.     
  255.     setstringlength (bs, len);
  256.     
  257.     moveleft (*htext, &bs [1], len);
  258.     } /*texthandletostring*/
  259.     
  260.     
  261. void lockhandle (h) Handle h; {
  262.     
  263.     if (h != nil)
  264.         HLock (h);
  265.     } /*lockhandle*/
  266.     
  267.     
  268. void unlockhandle (h) Handle h; {
  269.     
  270.     if (h != nil)
  271.         HUnlock (h);
  272.     } /*unlockhandle*/
  273.     
  274.     
  275. boolean newclearhandle (ctbytes, hreturned) long ctbytes; Handle *hreturned; {
  276.     
  277.     register long ct = ctbytes;
  278.     register Handle h;
  279.     
  280.     *hreturned = h = NewHandle (ct);
  281.     
  282.     if (h == nil) 
  283.         return (false);
  284.         
  285.     clearbytes (*h, ct);
  286.     
  287.     *hreturned = h;
  288.     
  289.     return (true);
  290.     } /*newclearhandle*/
  291.     
  292.     
  293. boolean newfilledhandle (ptrvoid pdata, long size, Handle *hreturned) {
  294.     
  295.     register Handle h;
  296.     register long ctbytes;
  297.     
  298.     ctbytes = size; 
  299.     
  300.     h = NewHandle (ctbytes);
  301.     
  302.     if (h == nil) {
  303.         
  304.         *hreturned = nil;
  305.         
  306.         return (false);
  307.         }
  308.     
  309.     moveleft (pdata, *h, ctbytes);
  310.         
  311.     *hreturned = h;
  312.     
  313.     return (true);
  314.     } /*newfilledhandle*/
  315.     
  316.     
  317. boolean newheapstring (bigstring bs, hdlstring *hstring) {
  318.  
  319.     return (newfilledhandle (bs, (long) stringlength (bs) + 1, (Handle *) hstring));
  320.     } /*newheapstring*/
  321.     
  322.     
  323. void copyheapstring (hdlstring hstring, bigstring bs) {
  324.     
  325.     if (hstring == nil) {
  326.         
  327.         setstringlength (bs, 0);
  328.         
  329.         return;
  330.         }
  331.     
  332.     lockhandle ((Handle) hstring);
  333.     
  334.     copystring (*hstring, bs);
  335.     
  336.     unlockhandle ((Handle) hstring);
  337.     } /*copyheapstring*/
  338.     
  339.  
  340. boolean sethandlecontents (void *p, long ct, Handle h) {
  341.  
  342.     sethandlesize (h, ct);
  343.     
  344.     if (MemError () != noErr)
  345.         return (false);
  346.     
  347.     moveleft (p, *h, ct);
  348.     
  349.     return (true);
  350.     } /*sethandlecontents*/
  351.  
  352.  
  353. boolean minhandlesize (Handle h, long size) {
  354.     
  355.     if (gethandlesize (h) >= size) /*already big enough*/
  356.         return (true);
  357.         
  358.     sethandlesize (h, size); /*try to make it larger*/
  359.     
  360.     return (MemError () == noErr);
  361.     } /*minhandlesize*/
  362.     
  363.     
  364. boolean newintarray (short ct, hdlintarray *harray) {
  365.     
  366.     Handle h;
  367.     
  368.     if (!newclearhandle (longsizeof (short) * ct, &h))
  369.         return (false);
  370.         
  371.     *harray = (hdlintarray) h;
  372.     
  373.     return (true);
  374.     } /*newintarray*/
  375.     
  376.  
  377. boolean setintarray (hdlintarray harray, short ix, short val) {
  378.     
  379.     /*
  380.     assign into a cell in a variable-size array of integers, 0-based index.
  381.     
  382.     return false if the array needed to be extended but there isn't enough
  383.     memory to do it.
  384.     */
  385.     
  386.     register hdlintarray h = harray;
  387.  
  388.     if (!minhandlesize ((Handle) h, (ix + 1) * longsizeof (short)))
  389.         return (false);
  390.     
  391.     (*harray) [ix] = val;
  392.     
  393.     return (true);
  394.     } /*setintarray*/
  395.     
  396.     
  397. boolean getintarray (hdlintarray harray, short ix, short *val) {
  398.     
  399.     *val = (*harray) [ix];
  400.     
  401.     return (true);
  402.     } /*getintarray*/
  403.     
  404.     
  405. void fillintarray (hdlintarray harray, short val) {
  406.     
  407.     register hdlintarray h = harray;
  408.     register short x = val;
  409.     register short ct;
  410.     
  411.     ct = gethandlesize ((Handle) h) / longsizeof (short);
  412.  
  413.     while (--ct    >= 0)
  414.         (*h) [ct] = x;
  415.     } /*fillintarray*/
  416.  
  417.  
  418.